//
//  FindTitleView.m
//  EjoyCar
//
//  Created by QingSong_Hao on 16/7/22.
//  Copyright © 2016年 石化盈科信息技术有限责任公司. All rights reserved.
//

#import "FindTitleView.h"

@implementation FindTitleView

- (id)initWithFrame:(CGRect)frame withArray:(NSArray *)itemArrays
{
    self = [super initWithFrame:frame];
    if (self) {
        _headerView = self;
        
        NSArray *itemArray = itemArrays;
        _btnCount = itemArray.count;
        
        // 可移动标记
        _banderBottomView = [[UIView alloc]initWithFrame:CGRectMake( ((SCREEN_WIDTH/_btnCount)-100)/2 ,_headerView.height - 4, 100, 3)];
        _banderBottomView.backgroundColor = HIGHLIGHT_COLOR;
        [_headerView addSubview:_banderBottomView];
        
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 1, self.width, 1)];
        v.backgroundColor = BMUIHexColor(0xCDCDCD);
        [_headerView addSubview:v];
        
        for(int i = 0;i<_btnCount;i++){
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            
            button.tag = 1000+i;
            if(i == 0){
                [button setTitleColor:HIGHLIGHT_COLOR forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:15];
            }else{
                [button setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:15];
            }
            button.frame = CGRectMake(i*(_headerView.width/_btnCount), 0, _headerView.width/_btnCount,_headerView.height);
            [button addTarget:self action:@selector(changeSelect:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:itemArray[i] forState:UIControlStateNormal];
            [_headerView addSubview:button];
        }
    }
    return self;
}

#pragma mark - 切换控制
-(void)changeSelect:(UIButton *)button
{
    for(int i = 0;i<_btnCount;i++){
        if(button.tag-1000 == i){
            [button setTitleColor:HIGHLIGHT_COLOR forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else{
            UIButton *bus = (UIButton *)[_headerView viewWithTag:1000+i];
            [bus setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
            bus.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }

    [UIView animateWithDuration:0.25 animations:^{
        _banderBottomView.frame = CGRectMake((button.tag-1000) *(SCREEN_WIDTH/_btnCount) + ((SCREEN_WIDTH/_btnCount)-100)/2 ,_headerView.height - 4, 100, 3);
    }];
    
    if(_itemClickBlock){
        _itemClickBlock(button);
    }
}

-(void)seleBtnIndex:(NSInteger)index{
    for(int i = 0;i<_btnCount;i++){
        if(index == i){
            UIButton *buss = [_headerView viewWithTag:1000+index];
            [buss setTitleColor:HIGHLIGHT_COLOR forState:UIControlStateNormal];
            buss.titleLabel.font = [UIFont systemFontOfSize:15];
        }else{
            UIButton *bus = (UIButton *)[_headerView viewWithTag:1000+i];
            [bus setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
            bus.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
    [UIView animateWithDuration:0.25 animations:^{
        _banderBottomView.frame = CGRectMake(index *(SCREEN_WIDTH/_btnCount) + ((SCREEN_WIDTH/_btnCount)-100)/2 ,_headerView.height - 4, 100, 3);
    }];
}

@end
